package sopia.conn;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
	   private final Context context;
	   private DatabaseHelper dbHelper ;
	   private SQLiteDatabase db;
	   // Contructor
	   public DBAdapter (Context ctx){
		   this.context = ctx;
		   dbHelper = new DatabaseHelper(this.context);
		   db = dbHelper.getWritableDatabase();
	   }   
	    //---closes the database---    
	    public void close() 
	    {	    	
	    	dbHelper.close();
	    }
	    
        public void setSqlDB(SQLiteDatabase sqlDB) {
			this.db = sqlDB;
		}

		public SQLiteDatabase getSqlDB() {
			return db;
		}

		private static class DatabaseHelper extends SQLiteOpenHelper 
        {
            DatabaseHelper(Context context) 
            {
                super(context, SQLQuery.DATABASE_NAME, null, SQLQuery.DATABASE_VERSION);
            }

            @Override
            public void onCreate(SQLiteDatabase db) 
            {
                db.execSQL(SQLQuery.SQL_CREATE_ACCOUNT_TABLE);
                db.execSQL(SQLQuery.SQL_CREATE_EXPENSE_TABLE);
                db.execSQL(SQLQuery.SQL_CREATE_CATEGORY_TABLE);
                db.execSQL(SQLQuery.SQL_CREATE_ITEM_EXPENSE_TABLE);
            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, 
                                  int newVersion) 
            {
                Log.w(SQLQuery.TAG, "Upgrading database from version " + oldVersion 
                      + " to "
                      + newVersion + ", which will destroy all old data");
                db.execSQL("DROP TABLE IF EXISTS " + SQLQuery.DB_ACCOUNT_TABLE);
                db.execSQL("DROP TABLE IF EXISTS " + SQLQuery.DB_EXPENSE_TABLE);
                db.execSQL("DROP TABLE IF EXISTS " + SQLQuery.DB_CATEG0RY_TABLE);
                db.execSQL("DROP TABLE IF EXISTS " + SQLQuery.DB_ITEM_EXPENSE_TABLE);
                onCreate(db);
            }
        }    
}
